package org.scc.service;

import java.util.List;

import org.scc.dao.UserDao;
import org.scc.model.User;
import org.scc.util.EncryptUtil;
import org.scc.vo.PageVO;

public class UserService {
	private UserDao userDao;
	
	public UserService(){
		userDao = new UserDao();
	}
	
	public User login(String username, String password){
		return userDao.login(username, getEncryptPassword(password) );
	}
	
	public User load(int id){
		return userDao.load(id);
	}
	
	public void update(User user){
		userDao.update(user);
	}
	
	public void delete(String ids){
		userDao.delete(ids);
	}
	
	public void add(User user){
		userDao.add(user);
	}
	
	/**
	 * 分页查询用户
	 * @param rowBeginIndex
	 * @param rowCount
	 * @return
	 */
	public PageVO<User> findPage(int rowBeginIndex, int rowCount){
		int total = userDao.getCount();
		List<User> users = userDao.findPage(rowBeginIndex, rowCount);
		PageVO<User> pageVO = new PageVO<User>();
		pageVO.setTotal(total);
		pageVO.setData(users);
		return pageVO;
	}
	
	/**
	 * 取得下一个用户名
	 * 将后面的数字加1
	 * @return
	 */
	public String getNextUsername(){
		String username = userDao.getMaxUsername();		//S1001
		String num = username.substring(1, username.length());	//1001
		return "S" + (Integer.parseInt(num) + 1 );//S1002
	}
	
	/**
	 * 取得默认密码为111111
	 * @return
	 */
	public String getDefaultPassword(){
		return new EncryptUtil().encrypt("111111");
	}
	
	public String getEncryptPassword(String password){
		return new EncryptUtil().encrypt(password);
	}
	
	public List<User> searchUser(String name){
		return userDao.searchUser(name);
	}
	
	public List<User> findUserIn(){
		return userDao.findUsersIn();
	}
	
	public List<User> findNoGroupUsers(){
		return userDao.findNoGroupUsers();
	}
}
